/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.vacoor.nothing.web.entity.Idable;

import java.io.Serializable;
import java.lang.reflect.Type;

import static org.vacoor.nothing.common.reflect.TypeResolver.resolveActualTypeArguments;

/**
 * 每一个项目都应该有一个 BaseEntity, BaseController, BaseService
 * Crud 是不可修改的, 对于每个项目使用 Base 有利于新增公共内容时在 Base 中来处理
 *
 * @author vacoor
 */
public abstract class ConventionController<E extends Idable<ID>, ID extends Serializable> extends ControllerSupport {

    @RequestMapping
    public String __default__() {
        return redirectViewPath("/");
    }

    @RequestMapping("/")
    public String index() {
        return getViewPath("index");
    }

    /* *****************************
     *
     * *****************************/

    protected final Class<E> entityClass;

    protected ConventionController() {
        Type typeArg = resolveActualTypeArguments(getClass(), ConventionController.class, 0);
        if (null == typeArg) {
            throw new IllegalStateException("Internal error: without actual type information");
        }

        if (!(typeArg instanceof Class)) {
            throw new IllegalStateException("Internal error: type parameter must be a certain type");
        }
        entityClass = (Class<E>) typeArg;
    }
}
